package com.byx.chatclient.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.byx.chatclient.R;
import com.byx.chatclient.utils.GlideUtils;
import com.github.chrisbanes.photoview.PhotoView;

import me.relex.photodraweeview.PhotoDraweeView;
import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * @ClassName BigImageView
 * @Description 点击缩略图显示大图
 * @Author Superb
 * @Date 2021/5/23
 * @Version 1.0
 */
public class BigImageView {
    private Context context;
    private static BigImageView instance;

    private BigImageView(Context context) {
        this.context = context;
    }

    public static BigImageView create(Context context){
        if (instance == null){
            instance = new BigImageView(context);
        }else if (instance.context != context){
            instance.context = context;
        }
        return instance;
    }

    public void showPhotoViewWithGlide(String imgPath){
        // 全屏显示的方法
        final Dialog dialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar);
        dialog.setContentView(R.layout.layout_photo_glide);
        PhotoView photoView = dialog.findViewById(R.id.pv_photo);
        photoView.setMaximumScale(5.0f);
        GlideUtils.loadImage(context,imgPath,photoView);
        dialog.show();
        // 大图显示之后，点击图片消失
        photoView.setOnClickListener(v -> dialog.dismiss());
    }

    public void showPhotoViewWithFresco(String uri){
        // 全屏显示的方法
        final Dialog dialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar);
        dialog.setContentView(R.layout.layout_photoview_fresco);
        PhotoDraweeView photoView = dialog.findViewById(R.id.pv_photo);
        photoView.setMaximumScale(5.0f);
        photoView.setPhotoUri(Uri.parse(uri),context);
        dialog.show();
        // 大图显示之后，点击整个view消失
        photoView.setOnViewTapListener((view, x, y) -> dialog.dismiss());
    }

    public void show(String imgPath){
        // 全屏显示的方法
        final Dialog dialog = new Dialog(context, R.style.TransparentStatusBarThemeBlack);
        ImageView imgView = getView(imgPath);
        dialog.setContentView(imgView);
        dialog.show();
        // 大图显示之后，点击图片消失
        imgView.setOnClickListener(v -> dialog.dismiss());
    }

    //设置当前imgView的图片
    private ImageView getView(String imgPath) {
        ImageView imgView = new ImageView(context);
        imgView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        imgView.setImageBitmap(decodeBigPhoto(imgPath));
        return imgView;
    }

    private Bitmap decodeBigPhoto(String imagePath){
        if (imagePath != null) {
            Bitmap bigBitmap;
            BitmapFactory.Options opt = new BitmapFactory.Options();
            bigBitmap = BitmapFactory.decodeFile(imagePath);
            opt.inSampleSize = 1;
            opt.inJustDecodeBounds = false;

            bigBitmap = BitmapFactory.decodeFile(imagePath, opt);
            return bigBitmap;
        }
        return null;

    }

    public static void unbind(){
        if (instance != null){
            instance.context = null;
        }
    }
}
